package com.zut.hospital.controller.pharmacy;

/**
 * @program: hos-test
 * @description:
 * @author: 杨曼珍
 * @create: 2019-06-02 21:18
 **/


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.zut.hospital.config.MedicineExcelHandler;
import com.zut.hospital.config.PurchaseExcelHandler;
import com.zut.hospital.entity.pharmacy.ExcelMedicine;
import com.zut.hospital.entity.pharmacy.ExcelPurchase;
import com.zut.hospital.service.pharmacy.MedicineService;
import com.zut.hospital.service.pharmacy.PurchaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;

import javax.servlet.http.HttpSession;

@RestController
public class ExcelImportController {
    @Autowired
    PurchaseService purchaseService;
    @Autowired
    MedicineService medicineService;

    private static final Logger log = LoggerFactory.getLogger(ExcelImportController.class);

    @PostMapping("excelImport.do")
    public String excelImport(@RequestParam(value = "file",required = false) MultipartFile file,HttpSession session) {
        ImportParams importParams = new ImportParams();
        // 数据处理
        IExcelDataHandler<ExcelPurchase> handler = new PurchaseExcelHandler();
        handler.setNeedHandlerFields(new String[] { "姓名" });// 注意这里对应的是excel的列名。也就是对象上指定的列名。
        importParams.setDataHanlder(handler);

        // 需要验证
        importParams.setNeedVerfiy(true);

        try {
            ExcelImportResult<ExcelPurchase> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ExcelPurchase.class,
                    importParams);

            List<ExcelPurchase> successList = result.getList();
            List<ExcelPurchase> failList = result.getFailList();

            log.info("是否存在验证未通过的数据:" + result.isVerfiyFail());
            log.info("验证通过的数量:" + successList.size());
            log.info("验证未通过的数量:" + failList.size());


            for (ExcelPurchase user : failList) {
                log.info("失败列表信息:" + user.getPchMdcName());
            }
            for (ExcelPurchase user : successList) {
                //插入数据库
                 log.info("成功！"+user.getId());
                 int a=purchaseService.insertPurchase(user.getPchMdcName(),user.getPchPrice(),user.getPchCount(),user.getPchDate(),user.getPchRemark(),user.getPchSupplier(),session.getAttribute("adminUser").toString());
                System.out.println("信息"+user.getPchMdcName()+user.getPchPrice()+user.getPchCount()+user.getPchDate()+user.getPchRemark()+user.getPchSupplier()+session.getAttribute("adminUser").toString());
            if(a==0)
            {
                    Map<String, Object> layuiMap = new HashMap<>();
                    layuiMap.put("code", 0);
                    layuiMap.put("msg", "failed");
                    layuiMap.put("count", 0);
                    layuiMap.put("data", null);
                     return JSON.toJSONString(layuiMap);
            }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

            Map<String, Object> layuiMap = new HashMap<>();
            layuiMap.put("code", 1);
            layuiMap.put("msg", "success");
            layuiMap.put("count", 0);
            layuiMap.put("data", null);
            return JSON.toJSONString(layuiMap);

    }
    @PostMapping("excelImportMed.do")
    public String excelImportMed(@RequestParam(value = "file",required = false) MultipartFile file) {
        ImportParams importParams = new ImportParams();
        // 数据处理
        IExcelDataHandler<ExcelMedicine> handler = new MedicineExcelHandler();
        handler.setNeedHandlerFields(new String[] { "姓名" });// 注意这里对应的是excel的列名。也就是对象上指定的列名。
        importParams.setDataHanlder(handler);

        // 需要验证
        importParams.setNeedVerfiy(true);

        try {
            ExcelImportResult<ExcelMedicine> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ExcelMedicine.class,
                    importParams);

            List<ExcelMedicine> successList = result.getList();
            List<ExcelMedicine> failList = result.getFailList();

            log.info("是否存在验证未通过的数据:" + result.isVerfiyFail());
            log.info("验证通过的数量:" + successList.size());
            log.info("验证未通过的数量:" + failList.size());


            for (ExcelMedicine user : failList) {
                log.info("失败列表信息:" + user.getMdcName());
            }
            for (ExcelMedicine user : successList) {
                //插入数据库
                log.info("成功！"+user.getId());
                int a=medicineService.insertMed(user.getMdcName(),user.getMdcUnit(),user.getMdcSpec(),user.getMdcCompany(),user.getMdcRemark());
                if(a==0)
                {
                    Map<String, Object> layuiMap = new HashMap<>();
                    layuiMap.put("code", 0);
                    layuiMap.put("msg", "failed");
                    layuiMap.put("count", 0);
                    layuiMap.put("data", null);
                    return JSON.toJSONString(layuiMap);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        Map<String, Object> layuiMap = new HashMap<>();
        layuiMap.put("code", 1);
        layuiMap.put("msg", "success");
        layuiMap.put("count", 0);
        layuiMap.put("data", null);
        return JSON.toJSONString(layuiMap);

    }
}
